package com.ihome.android.market2.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnKeyListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihome.android.image.CubeImageView;
import com.ihome.android.image.ImageLoader;
import com.ihome.android.academy.R;
import com.ihome.android.market2.info.AppInfo;

@SuppressLint("NewApi")
public class CategoryAppItem implements OnFocusChangeListener {
	private final String	TAG	= CategoryAppItem.class.getSimpleName();
	private RelativeLayout	mRelativeLayout;
	private ImageView		iv_frame;
	private Context			context;
	private ImageLoader		imageLoader;

	public CategoryAppItem(Context context, ImageLoader imageLoader) {
		this.context = context;
		this.imageLoader = imageLoader;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRelativeLayout = (RelativeLayout) inflater.inflate(R.layout.category_app_item, null);
		iv_frame = (ImageView) mRelativeLayout.findViewById(R.id.iv_frame);
		mRelativeLayout.setClickable(true);
		mRelativeLayout.setFocusable(true);
		mRelativeLayout.setFocusableInTouchMode(true);
		mRelativeLayout.setOnFocusChangeListener(this);

	}

	
	public void initView(AppInfo appInfo){
		if(appInfo != null){
			CubeImageView imgicon = (CubeImageView) mRelativeLayout.findViewById(R.id.item_icon);
			TextView txtname = (TextView) mRelativeLayout.findViewById(R.id.item_appname);
			TextView txtsec = (TextView) mRelativeLayout.findViewById(R.id.item_sec_info);
			TextView txtsize = (TextView) mRelativeLayout.findViewById(R.id.item_size);
			
			if (appInfo != null) {
				//LogUtil.d(TAG, " --> AppInfo = " + appInfo.toString());

				txtname.setText(appInfo.getAppName());
				txtsec.setText(context.getResources().getString(R.string.usernum, appInfo.getUserNum()));
				txtsize.setText(appInfo.getApkFileSize());
				
				if (!TextUtils.isEmpty(appInfo.getIconUrl())) {
					imgicon.loadImage(imageLoader, appInfo.getIconUrl());
				}
				else {
					imgicon.setBackgroundResource(R.drawable.icon_default);
				}
			}
		}
	}
	
	public void setClickListener(OnClickListener onClickListener) {
		mRelativeLayout.setOnClickListener(onClickListener);
	}

	public View getView() {
		return mRelativeLayout;
	}

	public void setLayoutParams(RelativeLayout.LayoutParams params) {
		mRelativeLayout.setLayoutParams(params);
	}

	public void setTag(Object tag) {
		mRelativeLayout.setTag(tag);
	}
	
	public void setOnKeyListener(OnKeyListener onkeylistener){
		mRelativeLayout.setOnKeyListener(onkeylistener);
	}

	@Override
	public void onFocusChange(View v, boolean hasFocus) {
		if (hasFocus) {
			iv_frame.setVisibility(View.VISIBLE);
		}
		else {
			iv_frame.setVisibility(View.INVISIBLE);
		}
	}
}
